﻿using ASmile.Com;
using ASmile.DBModel;
using ASmile.Extend;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmDictArea : FrmBase
    {
        public FrmDictArea()
        {
            InitializeComponent();
        }

        DictAreaSearch SearchArg { get; } = new DictAreaSearch();

        IBLLCrud<DictAreaSearch, Dict_Area> BLLObj { get; } = ObjectFactory.GetObject<IBLLCrud<DictAreaSearch, Dict_Area>>("DictAreaBLL");

        void RefreshTree()
        {
            tv.Nodes.Clear();
            SearchArg.AreaLevel = new int[] { 1, 2 };
            SearchArg.PageNo = 0;
            var rList = BLLObj.GetList(SearchArg).data;
            var rootNode = tv.Nodes.Add("全部");
            var level1List = rList.FindAll(s => s.AreaLevel == 1);
            if (level1List == null) return;
            foreach (var item1 in level1List)
            {
                var level1Node = rootNode.Nodes.Add(item1.AreaName);
                level1Node.Name = item1.AreaCode;
                var level2List = rList.FindAll(s => s.AreaLevel == 2 && s.PCode == item1.AreaCode);
                if (level2List == null) continue;
                foreach (var item2 in level2List)
                {
                    var level2Node = level1Node.Nodes.Add(item2.AreaName);
                    level2Node.Name = item2.AreaCode;
                }
            }
            rootNode.Expand();
        }

        void RefreshDgvData()
        {
            if (tv.SelectedNode == null)
            {
                dgv.DataSource = null;
                return;
            }
            GetPageData(1, ucDgvPage.OnePageCount, out int total);
            ucDgvPage.InitiControl(total);
        }

        private void GetPageData(int pageNo, int pageSize, out int total)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = tsTxtSearch.Text;
            SearchArg.AreaLevel = null;
            SearchArg.PCode = tv.SelectedNode?.Name;
            var ret = BLLObj.GetList(SearchArg);
            total = ret.count;
            dgv.DataSource = ret.data;
        }

        private void FrmDictArea_Load(object sender, EventArgs e)
        {
            RefreshTree();
            ucDgvPage.PageChangedEvent += GetPageData;
        }

        private void tsBtnRefesh_Click(object sender, EventArgs e)
        {
            RefreshTree();
        }

        private void tv_AfterSelect(object sender, TreeViewEventArgs e)
        {
            RefreshDgvData();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            if (tv.SelectedNode == null) return;
            FrmDictAreaEdit frm = new FrmDictAreaEdit();
            Dict_Area model = new Dict_Area
            {
                PCaption = tv.SelectedNode.FullPath,
                PCode = tv.SelectedNode.Name,
                AreaLevel = tv.SelectedNode.Level + 1
            };
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SaveHandle = m => BLLObj.Add(m as Dict_Area),
                SetModel = model
            });
            if (ret)
            {
                model.AreaCode = ret.data;
                dgv.AddDataSource(model);
                TreeNode pNode;
                if (model.PCode.IsNotEmpty())
                {
                    pNode = tv.FindNode(model.PCode);
                }
                else
                {
                    pNode = tv.Nodes[0];
                }
                pNode.Nodes.Add(model.AreaName).Name = model.AreaCode;
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            if (tv.SelectedNode == null) return;
            var model = dgv.GetSelectedClassData<Dict_Area>();
            if (model == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            model.PCaption = tv.SelectedNode.FullPath;
            FrmDictAreaEdit frm = new FrmDictAreaEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                IsEditShow = true,
                SaveHandle = m => BLLObj.Update(m as Dict_Area),
                SetModel = model
            });
            if (ret)
            {
                dgv.Refresh();
                tv.FindNode(model.AreaCode).Text = model.AreaName;
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            var selModel = dgv.GetSelectedClassData<Dict_Area>();
            if (selModel == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            if (!ShowQuestion($"确实要移除项目 {selModel.AreaName} 吗？", "移除确认")) return;
            var ret = BLLObj.Del(selModel);
            if (ret)
            {
                dgv.DelDataSource(selModel);
                tv.FindNode(selModel.AreaCode).Remove();
            }
            ShowMessage(ret.msg);
        }

        private void tsAdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, RefreshDgvData, dgv);
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshDgvData();
            }
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            RefreshDgvData();
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var r = dgv.GetSelectedClassData<Dict_Area>();
            if (r == null) return;
            tv.SelectedNode = tv.FindNode(r.AreaCode);
        }
    }
}